#### Read in raw reptile survey data

rawdata = read.csv('/home1/99/jc152199/Abundance/cjs_raw_R_std_survey_data.csv',header=T)

#### Dates and times look good, but will need adjust, I think it can be done in R

str(rawdata)

### Split date into year, month, and day

splitlist = (as.character(rawdata$date),'/')

### Loop through splitlist, populating columns with information
### PS, this is a fucking garbage way to perform this task and takes WAY TOO LONG
### Next time, figure out how to use apply or some bullshit like that

rawdata$day=NA
rawdata$month=NA
rawdata$year=NA

for (i in c(1:length(splitlist)))

	{
	
	### Select a single row
	
	t.list = splitlist[[i]]
	
	if(length(t.list)>0)
	
	{
	
	### Populate date info
	
	rawdata$day[i] = sprintf('%02i',as.numeric(t.list[1]))
	rawdata$month[i] = sprintf('%02i',as.numeric(t.list[2]))
	rawdata$year[i] = as.numeric(substr(t.list[3],1,4))
	
	}
	
	cat('\n',i,'\n')
	
	}
	
# Done

### Change structure of original date column

rawdata$date = as.character(rawdata$date)

### Replace with YYYYMMDD

rawdata$date = paste(rawdata$year,sprintf('%02i',rawdata$month),sprintf('%02i',rawdata$day),sep='')

### Write out

write.csv(rawdata,file=paste('/home1/99/jc152199/Abundance/SurveyDataRawDatesFixed.csv',sep=''),row.names=F)

### Now adjust times
### First convert from factor to character

rawdata$start = as.character(rawdata$start)
rawdata$finish = as.character(rawdata$finish)

### Remove '30/12/1899 ' and replace with ''

rawdata$start = gsub('30/12/1899 ','',rawdata$start)
rawdata$finish = gsub('30/12/1899 ','',rawdata$finish)

### Remove last 3 characters (which represent seconds)

rawdata$start = substr(rawdata$start,1,nchar(rawdata$start)-3)
rawdata$finish = substr(rawdata$finish,1,nchar(rawdata$finish)-3)

### Now split then recombine to make sure there's always a leading zero

rawdata$starthour = sprintf('%02i',as.numeric(gsub(':','',substr(rawdata$start,1,2))))
rawdata$finishhour = sprintf('%02i',as.numeric(gsub(':','',substr(rawdata$finish,1,2))))

rawdata$startmins = substr(rawdata$start,nchar(rawdata$start)-1,nchar(rawdata$start))
rawdata$finishmins = substr(rawdata$finish,nchar(rawdata$finish)-1,nchar(rawdata$finish))

rawdata$start = paste(rawdata$starthour,':',rawdata$startmins,sep='')
rawdata$finish = paste(rawdata$finishhour,':',rawdata$finishmins,sep='')

### Write out

write.csv(rawdata,file=paste('/home1/99/jc152199/Abundance/SurveyDataRawDatesAndTimesFixed.csv',sep=''),row.names=F)
rawdata = read.csv('/home1/99/jc152199/Abundance/SurveyDataRawDatesAndTimesFixed.csv',header=T)

### Dates and times are not formatted to POSixct or any bullshit like that
### Just characters that are all the same format

### Need a way to vet for good samples before counting samples\
### Try plotting up abundance of each species within a site against sample variables (airtemp, subtemp, cloud, etc...)
### First step is summarizing species abundances within a site
### Test georef_id 12252 with 14 samples

g.id = 12252

abund.range = NULL

outdata = NULL

i=1

for (g.id in unique(rawdata$georef_ID))

	{
	
	### Subset to a single site
	
	tdata = rawdata[which(rawdata$georef_ID==g.id),]
	
	### A list of all species observed at this site across all samples
	
	t.spp = unique(tdata$species,na.rm=T)
	
	### Position tracker
	
	i=i+1
	
	for (s.id in unique(tdata$sample_ID))
	
		{
		
		### Subset to a single sample
		
		ttdata = tdata[which(tdata$sample_ID==s.id),]
		
		### Now loop through species which have been observed at the site
		
		for (spp in t.spp)
		
			{
			
			#### Subset to a single species
			
			tttdata = ttdata[which(ttdata$species==spp),]
			
			### Build a dataframe that lists species by abundance with sample characteristics
			
			t.out = data.frame(spp=spp, abund=nrow(tttdata), unique(ttdata[,c(1:5,7:22,25)],na.rm=T))
			
			### Bind to outdata
			
			outdata = rbind(t.out, outdata)
			
			}
	
		}
		
	### Track progress
	
	cat('\n',i/length(unique(rawdata$georef_ID,na.rm=T))*100,'- Percent Complete\n')
		
	}
	
## Done

### Write out summarized abundance data

write.csv(outdata, file=paste('/home1/99/jc152199/Abundance/AbundanceBySample.csv',sep=''),row.names=F)